﻿@using DCMS.Web.Extensions;
@using DCMS.Web.Framework;
@using DCMS.Web.Framework.UI;
@using DCMS.ViewModel.Models.Finances;
@using DCMS.ViewModel.Models.Configuration;
@using DCMS.Core;
@using DCMS.Core.Infrastructure;

@model LedgerDetailsListModel

@{
    var _webHelper = EngineContext.Current.Resolve<IWebHelper>();
    int.TryParse(Context.Request?.Query["optionid"], out int curOptionId);
}

<section id="content_wrapper">

    <!-- Start: Topbar-Dropdown -->
    <div id="topbar-dropmenu">
        @await Html.PartialAsync("_ToolBox")
    </div>
    <!-- End: Topbar-Dropdown -->
    <!-- Start: Topbar -->
    <header id="topbar">
        <div class="topbar-left">
            <ol class="breadcrumb">
                <li class="crumb-active">
                    <a href="#">财务</a>
                </li>
                <li class="crumb-icon">
                    <a href="@Url.RouteUrl("HomePage")">
                        <span class="glyphicon glyphicon-home"></span>
                    </a>
                </li>
                <li class="crumb-link">
                    <a href="#">财务报表</a>
                </li>
                <li class="crumb-trail">明细分类账</li>
            </ol>
        </div>
        <div class="topbar-right">
            <div class="ml15 ib va-m" id="toggle_sidemenu_r">
                <a href="#" class="pl5">
                    <i class="fa fa-sign-in fs22 text-primary"></i>
                    <span class="badge badge-hero badge-danger">3</span>
                </a>
            </div>
        </div>
    </header>
    <!-- End: Topbar -->
    <!-- Begin: Content -->
    <section id="content" class="formSection">

        <div class="special-alerts">
            <div class="alert alert-danger light alert-dismissable" id="alert-demo-2" style="display: none;">
                <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
                <i class="fa fa-info pr10"></i>
                <strong>您没有被授权此操作!</strong>
            </div>
        </div>

        <div class="row">
            <div class="col-md-12">
                <form asp-action="List" asp-controller="LedgerDetails" method="get" id="formSearch" novalidate="novalidate">
                    <div class="panel panel-default" id="panel0">
                        <div class="panel-heading">
                            <span class="glyphicon glyphicon-search"></span> 高级搜索
                            <a class="btn btn-default btn-sm pull-right m5 mrn p5 pbn" id="AdvancedSearch"><i class="fa fa-angle-double-right"></i></a>
                        </div>
                        <div class="panel-body p10">
                            <div class="row">
                                <div class="col-sm-2">
                                    <dcms-editor asp-for="StartTime" placeholder="开始..." />
                                </div>
                                <div class="col-sm-2">
                                    <dcms-editor asp-for="EndTime" placeholder="结束..." />
                                </div>
                                <div class="col-sm-6">
                                    <button id="btn_query" type="submit" class="btn btn-primary btn-block">
                                        <span class="glyphicon glyphicon-search mr5" aria-hidden="true"></span>查询
                                    </button>
                                </div>
                                <div class="col-sm-1">
                                    <a id="btn_query" href="/trialbalance/list" class="btn btn-warning btn-block">
                                        <i class="fa fa-undo mr5"></i> 返回余额表
                                    </a>
                                </div>
                                <div class="col-sm-1">
                                    <a href="?cache=true" class="btn btn-info mr5 btn-block">
                                        <span class="glyphicon glyphicon-refresh mr5"></span>刷新
                                    </a>
                                </div>
                            </div>

                        </div>
                    </div>
                </form>

            </div>
        </div>


        <div class="row">
            <div class="col-md-2">
                <div class="panel">
                    <div class="panel-heading">
                        <div class="panel-title">
                            <span class="glyphicon glyphicon-tasks"></span> 科目列表
                        </div>
                    </div>
                    <div class="panel-body p10">
                        <table class="table table-striped table-bordered table-hover">
                            @foreach (var tree in Model.Trees.OrderBy(s=>s.Option.Code))
                            {
                                <tr>
                                    <td style="@(curOptionId==tree.Option.Id?"background-color:#4a89dc;color:#fff;":"")">
                                        @if (tree.Children.Count > 0)
                                        {
                                            <span class="glyphicon glyphicon-chevron-down pl15 mr10"></span>
                                            <a class="account-option" href="@Url.Action("list","ledgerdetails",new {starttime=Model.StartTime.ToString("yyyy-MM-dd"),endtime =Model.EndTime.ToString("yyyy-MM-dd"),optionid=tree.Option.Id })" data-id="@tree.Option.Id" style="@(curOptionId==tree.Option.Id?"color:#fff;":"")">@tree.Option.Name</a>
                                        }
                                        else
                                        {
                                            <span class="pl15 mr20"></span><a class="account-option" href="@Url.Action("list","ledgerdetails",new {starttime=Model.StartTime.ToString("yyyy-MM-dd"),endtime =Model.EndTime.ToString("yyyy-MM-dd"),optionid=tree.Option.Id })" data-id="@tree.Option.Id" style="@(curOptionId==tree.Option.Id?"color:#fff;":"")">@tree.Option.Name</a>
                                        }
                                    </td>
                                </tr>
                                if (tree.Children.Count > 0)
                                {
                                    foreach (var node in tree.Children)
                                    {

                                        if (node.Children.Count > 0)
                                        {
                                            <tr>

                                                @if (node.Children.Count > 0)
                                                {
                                                    <td style="@(curOptionId==node.Option.Id?"background-color:#4a89dc;color:#fff;":"")"><span style="padding-left:60px;" class="glyphicon glyphicon-chevron-down mr10"></span><a class="account-option" href="@Url.Action("list","ledgerdetails",new {starttime=Model.StartTime.ToString("yyyy-MM-dd"),endtime =Model.EndTime.ToString("yyyy-MM-dd"),optionid=node.Option.Id })" data-id="@node.Option.Id" style="@(curOptionId==node.Option.Id?"color:#fff;":"")">@node.Option.Name</a></td>
                                                }
                                                else
                                                {
                                                    <td style="@(curOptionId==node.Option.Id?"background-color:#4a89dc;color:#fff;":"")"><span style="padding-left:60px;"></span><a class="account-option" href="javascript:;" data-id="@node.Option.Id" style="@(curOptionId==node.Option.Id?"color:#fff;":"")">@node.Option.Name</a></td>
                                                }
                                            </tr>

                                            foreach (var child in node.Children)
                                            {
                                                if (child.Option.Selected)
                                                {
                                                    <tr>
                                                        <td style="@(curOptionId==child.Option.Id?"background-color:#4a89dc;color:#fff;":"")"><span style="padding-left:100px;"></span><a class="account-option" href="@Url.Action("list","ledgerdetails",new {starttime=Model.StartTime.ToString("yyyy-MM-dd"),endtime =Model.EndTime.ToString("yyyy-MM-dd"),optionid=child.Option.Id })" data-id="@child.Option.Id" style="@(curOptionId==node.Option.Id?"color:#fff;":"")">@child.Option.Name</a></td>
                                                    </tr>
                                                }
                                            }
                                        }
                                        else
                                        {
                                            <tr>
                                                @if (node.Children.Count > 0)
                                                {
                                                    <td style="@(curOptionId==node.Option.Id?"background-color:#4a89dc;color:#fff;":"")"><span style="padding-left:60px;" class="glyphicon glyphicon-chevron-down mr10"></span><a class="account-option" href="@Url.Action("list","ledgerdetails",new {starttime=Model.StartTime.ToString("yyyy-MM-dd"),endtime =Model.EndTime.ToString("yyyy-MM-dd"),optionid=node.Option.Id })" data-id="@node.Option.Id" style="@(curOptionId==node.Option.Id?"color:#fff;":"")">@node.Option.Name</a></td>
                                                }
                                                else
                                                {
                                                    <td style="@(curOptionId==node.Option.Id?"background-color:#4a89dc;color:#fff;":"")"><span style="padding-left:60px;"></span><a class="account-option" href="@Url.Action("list","ledgerdetails",new {starttime=Model.StartTime.ToString("yyyy-MM-dd"),endtime =Model.EndTime.ToString("yyyy-MM-dd"),optionid=node.Option.Id })" data-id="@node.Option.Id" style="@(curOptionId==node.Option.Id?"color:#fff;":"")">@node.Option.Name</a></td>
                                                }

                                            </tr>
                                        }

                                    }
                                }

                            }
                        </table>


                    </div>
                </div>
            </div>

            <div class="col-md-10">
                <div class="panel">
                    <div class="panel-heading">
                        <div class="panel-title">
                            <span class="glyphicon glyphicon-tasks"></span> 明细账
                        </div>
                    </div>
                    <div class="panel-body p10">

                        <table class="table table-striped table-bordered table-hover">
                            <tr>
                                <th class="text-center w50">序号</th>
                                <th class="text-left">日期</th>
                                <th class="text-left">凭证字号</th>
                                <th class="text-left">摘要</th>
                                <th class="text-right" width="15%">借方</th>
                                <th class="text-right" width="15%">贷方</th>
                                <th class="text-center">方向</th>
                                <th class="text-right" width="15%">余额</th>
                            </tr>

                            <tr>
                                <td class="text-center">1</td>
                                <td class="text-weight">期初余额</td>
                                <td></td>
                                <td></td>
                                <td class="text-right text-weight">￥ @((Model.StartBalanceDebitAmount ?? 0).ToString("#,##0.00"))</td>
                                <td class="text-right text-weight">￥ @((Model.StartBalanceCreditAmount ?? 0).ToString("#,##0.00"))</td>
                                <td class="text-center text-weight">@Model.StartBalanceDirection</td>
                                <td class="text-right text-weight">￥ @((Model.StartBalanceAmount ?? 0).ToString("#,##0.00"))</td>
                            </tr>

                            @{
                                int i = 2;
                                foreach (var group in Model.GroupMonths)
                                {
                                    @foreach (var item in group.Items)
                                    {
                                        <tr>
                                            <td class="text-center">@(i++)</td>
                                            <td>@item.RecordTime.ToString("yyyy-MM-dd")</td>
                                            <td>
                                                @*<a href="@Url.Action("view","recordingvoucher",new { id= item.RecordingVoucherId,returnUrl= _webHelper.GetThisPageUrl(true) })"> @($"{item.RecordName}-{item.RecordNumber}")</a>*@
                                                <a href="javascript:;" class="ShowRecordingvoucher" data-src="#modal-text_@item.RecordingVoucherId"> @($"{item.RecordName}-{item.RecordNumber}")</a>
                                                <div id="modal-text_@item.RecordingVoucherId" class="popup-basic p25 mfp-with-anim mfp-hide">
                                                    <table class="table">
                                                        <tr>
                                                            <th class="text-left">科目</th>
                                                            <th class="text-right">借</th>
                                                            <th class="text-right">贷</th>
                                                        </tr>
                                                        @foreach (var vi in item.Items)
                                                        {
                                                            <tr>
                                                                <td class="text-left">@vi.AccountingOptionName</td>
                                                                <td class="text-right">￥ @((vi?.DebitAmount ?? 0).ToString("#,##0.00"))</td>
                                                                <td class="text-right">￥ @((vi?.CreditAmount ?? 0).ToString("#,##0.00"))</td>
                                                            </tr>
                                                        }
                                                    </table>
                                                </div>
                                            </td>
                                            <td>
                                                <a href="@item.BillLink">
                                                    @item.BillNumber
                                                </a>
                                            </td>
                                            <td class="text-right">￥ @((item.DebitAmount ?? 0).ToString("#,##0.00"))</td>
                                            <td class="text-right">￥ @((item.CreditAmount ?? 0).ToString("#,##0.00"))</td>
                                            <td class="text-center">@item.Direction</td>
                                            <td class="text-right">￥ @((item.Balances ?? 0).ToString("#,##0.00"))</td>
                                        </tr>
                                    }

                                    <tr>
                                        <td class="text-center">@(i++)</td>
                                        <td class="text-weight">本期合计</td>
                                        <td></td>
                                        <td></td>
                                        <td class="text-right text-weight">￥ @((group.CurBalanceDebitAmount ?? 0).ToString("#,##0.00"))</td>
                                        <td class="text-right text-weight">￥ @((group.CurBalanceCreditAmount ?? 0).ToString("#,##0.00"))</td>
                                        <td class="text-center text-weight">@group.CurBalanceDirection</td>
                                        <td class="text-right text-weight">￥ @((group.CurBalanceAmount ?? 0).ToString("#,##0.00"))</td>
                                    </tr>
                                    <tr>
                                        <td class="text-center">@(i++)</td>
                                        <td class="text-weight">本年合计</td>
                                        <td></td>
                                        <td></td>
                                        <td class="text-right text-weight">￥ @((group.YearBalanceDebitAmount ?? 0).ToString("#,##0.00"))</td>
                                        <td class="text-right text-weight">￥ @((group.YearBalanceCreditAmount ?? 0).ToString("#,##0.00"))</td>
                                        <td class="text-center text-weight">@group.YearBalanceDirection</td>
                                        <td class="text-right text-weight">￥ @((group.YearBalanceAmount ?? 0).ToString("#,##0.00"))</td>
                                    </tr>
                                }
                            }

                        </table>

                    </div>
                </div>


            </div>
        </div>

    </section>

    <!-- End: Content -->
</section>

@*template-user-events*@
@*<script id="template-ledger-events" type="text/x-tmpl">
        {% if(o.data!=null){ %}
        <table class="table table-striped table-bordered table-hover">
            <tr>
                <th class="text-center w50">序号</th>
                <th class="text-left">日期</th>
                <th class="text-left">凭证字号</th>
                <th class="text-left">摘要</th>
                <th class="text-right">借方</th>
                <th class="text-right">贷方</th>
                <th class="text-left">方向</th>
                <th class="text-right">余额</th>
            </tr>

            {% for (var i=0,details;details=o.data[i]; i++) { %}
            <tr>
                <td class="text-center">{%=i+1%}</td>
                <td class="text-left">
                    {% if(details.RecordTime!='0001-01-01T00:00:00' && details.RecordTime!='1-01-01 01:00:00'){ %}
                    {%=details.RecordTime%}{% }
                    %}
                </td>
                <td>{%=details.RecordName%}</td>
                <td>{%=details.Summary%}</td>
                <td class="text-right">￥ {%=details.DebitAmount%}</td>
                <td class="text-right">￥ {%=details.CreditAmount%}</td>
                <td class="text-left">{%=details.Direction%}</td>
                <td class="text-right">￥ {%=details.Balances%}</td>
            </tr>
            {% } %}
        </table>
        {% } %}
    </script>*@


@{
    //Html.AddCssFileParts(ResourceLocation.Head, "~/assets/skin/default_skin/css/theme.css");
    //Html.AddCssFileParts(ResourceLocation.Head, "~/Scripts/Toastr/toastr.css");
    //Html.AddCssFileParts(ResourceLocation.Head, "~/Scripts/Tables/bootstrap-table.css");
    //Html.AddCssFileParts(ResourceLocation.Head, "~/vendor/plugins/datepicker/css/bootstrap-datetimepicker.css");
    //Html.AddCssFileParts(ResourceLocation.Head, "~/vendor/plugins/daterange/daterangepicker.css");
    //Html.AddCssFileParts(ResourceLocation.Head, "~/vendor/plugins/colorpicker/css/bootstrap-colorpicker.min.css");
    //Html.AddCssFileParts(ResourceLocation.Head, "~/vendor/plugins/tagmanager/tagmanager.css");
    //Html.AddCssFileParts(ResourceLocation.Head, "~/vendor/plugins/fancytree/skin-win8/ui.fancytree.min.css");
}



@section CurPageScripts
{
    @{
//Html.AddScriptParts(ResourceLocation.Footer, "~/assets/js/utility/jquery.sparkline.min.js");
//Html.AddScriptParts(ResourceLocation.Footer, "~/Scripts/jquery.validate.min.js");
//Html.AddScriptParts(ResourceLocation.Footer, "~/Scripts/jquery.validate.unobtrusive.js");
//Html.AddScriptParts(ResourceLocation.Footer, "~/Scripts/Tables/jquery.treegrid.min.js");
//Html.AddScriptParts(ResourceLocation.Footer, "~/Scripts/Tables/bootstrap-table.js");
//Html.AddScriptParts(ResourceLocation.Footer, "~/Scripts/Tables/locale/bootstrap-table-zh-CN.min.js");
//Html.AddScriptParts(ResourceLocation.Footer, "~/Scripts/Tables/bootstrap-table-treegrid.js");
//Html.AddScriptParts(ResourceLocation.Footer, "~/Scripts/globalize.min.js");
//Html.AddScriptParts(ResourceLocation.Footer, "~/Scripts/moment.js");
//Html.AddScriptParts(ResourceLocation.Footer, "~/Scripts/moment-with-locales.js");
//Html.AddScriptParts(ResourceLocation.Footer, "~/vendor/plugins/daterange/daterangepicker.js");
//Html.AddScriptParts(ResourceLocation.Footer, "~/vendor/plugins/datepicker/js/bootstrap-datetimepicker.js");
//Html.AddScriptParts(ResourceLocation.Footer, "~/vendor/plugins/colorpicker/js/bootstrap-colorpicker.min.js");
//Html.AddScriptParts(ResourceLocation.Footer, "~/vendor/plugins/jquerymask/jquery.maskedinput.min.js");
//Html.AddScriptParts(ResourceLocation.Footer, "~/vendor/plugins/tagmanager/tagmanager.js");
//Html.AddScriptParts(ResourceLocation.Footer, "~/vendor/plugins/fancytree/jquery.fancytree-all.min.js");
//Html.AddScriptParts(ResourceLocation.Footer, "~/vendor/plugins/magnific/jquery.magnific-popup.js");
//Html.AddScriptParts(ResourceLocation.Footer, "~/assets/admin-tools/admin-forms/js/advanced/steps/jquery.steps.js");
//Html.AddScriptParts(ResourceLocation.Footer, "~/assets/admin-tools/admin-forms/js/jquery.validate.min.js");
//Html.AddScriptParts(ResourceLocation.Footer, "~/Scripts/Toastr/Toastr.min.js");
//Html.AddScriptParts(ResourceLocation.Footer, "~/Scripts/Custom/Common.js");
//Html.AddScriptParts(ResourceLocation.Footer, "~/assets/js/bootstrap/holder.min.js");
    }

    <script type="text/javascript">
        //这块需要追加到 Common.js
        ; (function ($) {
            $.fn.extend({
                //根据科目载入明细账
                loadLedgerDetails: function (optionId, start, end) {
                    $.ajax({
                        type: 'GET',
                        cache: true,
                        url: "/LedgerDetails/getLedgerDetailsByOptions?optionid=" + optionId + "&start=" + start + "&end=" + end,
                        dataType: "json",
                        beforeSend: function () {
                            $("#ledgerDetails").html("<img src='@LayoutExtensions.ResourceServerUrl("/Content/images/loading.gif")' />");
                        },
                        success: function (result) {
                            if (result.Data != null && result.Data.length > 0) {
                                $(this).showLedgerDetails(result.Data, $("#ledgerDetails"));
                            }
                            else {
                                $(this).showAlert("ledgerDetails", "抱歉，没有数据可供显示，请点击明细科目", false);
                            }
                        },
                        error: function (msg) {
                            $(this).showAlert("ledgerDetails", "抱歉，获取服务器端数据失败，您可以尝试刷新.", false);
                        }
                    });
                },
                //解析业务员
                showLedgerDetails: function (json, obj) {
                    var temps = tmpl("template-ledger-events");

                    $.each(json, function (k, v) {
                        v.RecordTime = changeDateFormat(v.RecordTime);
                    });

                    var result = this.renderTemplate(temps, json, obj);
                },
                //解析模板
                renderTemplate: function (func, data, obj) {
                    if (!func) {
                        return $();
                    }

                    var result = func({ data: data });
                    return obj.html(result).children();
                },
                //友好提示
                showAlert: function (obj, msg, close) {
                    var html = "<div class=\"alert alert-sm alert-border-left alert-danger light alert-dismissable\">";
                    if (close) {
                        html += "<button type=\"button\" class=\"close\" data-dismiss=\"alert\" aria-hidden=\"true\">×</button>"
                    }
                    html += "<i class=\"fa fa-info pr10\"></i><strong>" + msg + "</strong></div>";
                    $("#" + obj + "").html(html);
                }
            });

            function changeDateFormat(cellval) {
                // var d = new Date(cellval)
                //var a= d.getFullYear() + '-' + (d.getMonth() + 1 < 10 ? "0" + (d.getMonth() + 1) : d.getMonth() + 1) + '-' + (d.getDate()<10 ? "0" +d.getDate():d.getDate())+ ' ' + (d.getHours()<10 ?"0"+d.getDate():d.getDate()) + ':' + (d.getMinutes()<10 ? "0" + d.getMinutes(): d.getMinutes()) + ':' + (d.getSeconds()<10 ? "0" + d.getSeconds() : d.getSeconds() );
                //return a
                if (cellval != "" && cellval != null && typeof (cellval) != 'undefined' && cellval != '0001-01-01T00:00:00' && cellval != '1-01-01 01:00:00') {
                    var dateee = new Date(cellval).toJSON();
                    var date = new Date(+new Date(dateee) + 8 * 3600 * 1000).toISOString().replace(/T/g, ' ').replace(/\.[\d]{3}Z/, '');
                    date = date.replace('-', '/').replace('-', '/');
                    return date;
                }
                else {
                    return "";
                }

            };

        })(jQuery);

        jQuery(document).ready(function () {

            "use strict";

            // Init Theme Core
            Core.init();

            // Init Demo JS
            Demo.init();

            //权限提示
            if ($(this).getUrlQueryString("Authorize") == "noAuthorize") {
                $('#alert-demo-2').fadeToggle();
                setTimeout(function () {
                    $('#alert-demo-2').fadeToggle();
                }, 3000);
            }

            //高级搜索
            $("#panel0 #AdvancedSearch").click(function () {
                var ico = $(this).find(".fa");
                if (ico.hasClass('fa-angle-double-right'))
                    ico.removeClass("fa-angle-double-right").addClass("fa-angle-double-down");
                else
                    ico.removeClass("fa-angle-double-down").addClass("fa-angle-double-right");
                $("#panel0").find(".panel-body").toggleClass("hidden fadeIn");
            });

            var modalContent = $('#modal-content');

            modalContent.on('click', '.holder-style', function (e) {
                e.preventDefault();

                modalContent.find('.holder-style').removeClass('holder-active');
                $(this).addClass('holder-active');
            });

            function findActive() {
                var activeModal = modalContent.find('.holder-active').attr('href');
                return activeModal;
            };

            $('.ShowRecordingvoucher').on('click', function () {
                //console.log("ShowRecordingvoucher");
                var active = $(this).attr("data-src");
                $.magnificPopup.open({
                    removalDelay: 500, //delay removal by X to allow out-animation,
                    items: {
                        src: active
                    },
                    // overflowY: 'hidden', //
                    callbacks: {
                        beforeOpen: function (e) {
                            this.st.mainClass = "mfp-zoomIn";
                        }
                    },
                    midClick: true
                    // allow opening popup on middle mouse click. Always set it to true if you don't provide alternative source.
                });

            });

        });
    </script>
}


